package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SearchReader
 */
public class SearchReader extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String searchtype = request.getParameter("types");
		String searchinfo = request.getParameter("searchinfo");
		if (searchtype == null)
			searchtype = "phoneNumber";
		if (searchinfo == null)
			searchinfo = "";
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		try {
			Connection con = dbOperator.BasicOperation.getConnection();
			ResultSet rs;
			rs = dbOperator.AdvancedOperation.getReaderInfo(con, searchinfo, searchtype);
			while (rs.next()) {
				out.print("<tr>");
				String uid = rs.getString(1);
				out.println("<td><a href='' onclick='displayInfo(this)' data-toggle='modal' data-target='#displayinfo'>"
						+ uid + "</a></td>");
				out.println("<td id=\"ema" + uid + "\">" + rs.getString(2) + "</td>");
				out.println("<td id=\"pho" + uid + "\">" + rs.getString(3) + "</td>");
				out.print("<td><input type=\"button\" class=\"btn btn-primary\" value=\"edit\" name=\"edt" + uid
						+ "\" onclick=\"editReader(this)\" data-toggle='modal' data-target='#editReader'>");
				out.print("<input type=\"button\" class=\"btn btn-danger\" value=\"delete\" name=\"dlt" + uid
						+ "\" onclick=\"deleteReader(this)\" data-toggle='modal' data-target='#deleteReader'></td>");
				out.print("</tr>");
			}
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
